package com.kechen.dao;

import com.kechen.pojo.Employee;
import com.kechen.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmpDao {
    private Connection con= DBUtil.getConnection();
    //员工的插入功能
    public void insertEmp(Employee employee) throws Exception{

        String sql="insert into t_employee(name,age)values(?,?)";


            PreparedStatement ps =con.prepareStatement(sql);
            ps.setString(1, employee.getName());
            ps.setString(2, employee.getAge());
            //实现sql操作
            ps.executeUpdate();
            System.out.println("员工信息插入成功！");

            System.out.println("员工信息插入失败！");

    }


    public List<Employee> findEmps() {
        List<Employee> emps = new ArrayList<Employee>();
        String sql="select * from t_employee";

        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs= ps.executeQuery();
            while(rs.next()){
                Employee emp=new Employee();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setAge(rs.getString("age"));
                emps.add(emp);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return emps;
    }

    public static void main(String[] args) {
        EmpDao dao = new EmpDao();
        List<Employee> emps = dao.findEmps();
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    public void deleteEmpById(int id) {
        String sql="delete from t_employee where id=?";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            System.out.println(id);
            ps.executeUpdate();
            System.out.println("根据ID删除员工信息成功");
        } catch (SQLException e) {
            System.out.println("根据ID删除员工信息失败");
            e.printStackTrace();
        }
    }

    public Employee findEmpById(int id) {
        Employee emp=null;
        String sql="select * from t_employee where id=?";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                emp=new Employee();
                emp.setId(id);
                emp.setName(rs.getString("name"));
                emp.setAge(rs.getString("age"));
            }
            System.out.println("根据ID:"+id+"查询出员工信息");
        } catch (SQLException e) {
            System.out.println("根据ID:"+id+"查询员工信息失败");
            e.printStackTrace();
        }

        return emp;
    }

    public void updateEmp(Employee emp) {
        String sex=null;
        String sql="update t_employee set name=?,age=? where id=?";
        try {
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setString(2, emp.getAge());
            ps.setInt(3, emp.getId());
            ps.executeUpdate();
            System.out.println("员工信息修改成功");
        } catch (SQLException e) {
            System.out.println("员工信息修改失败");
            e.printStackTrace();
        }
    }

}
